package com.lmall.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmall.common.exception.BadRequestException;
import com.lmall.user.config.JwtProperties;
import com.lmall.user.domain.dto.UserLoginDTO;
import com.lmall.user.domain.dto.UserRegisterDTO;
import com.lmall.user.domain.po.User;
import com.lmall.user.domain.po.UserInfo;
import com.lmall.user.domain.vo.UserLoginVO;
import com.lmall.user.domain.vo.UserRegisterVO;
import com.lmall.user.mapper.UserMapper;
import com.lmall.user.service.IUserService;
import com.lmall.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final PasswordEncoder passwordEncoder;

    private final JwtTool jwtTool;

    private final JwtProperties jwtProperties;

    @Override
    public UserRegisterVO register(UserRegisterDTO userRegisterDTO) {
        // 1、校验参数
        String username = userRegisterDTO.getUsername();
        String password = userRegisterDTO.getPassword();
        String email = userRegisterDTO.getEmail();
        // 2、根据用户名查询用户
        User user = lambdaQuery().eq(User::getUsername, username).one();
        if (user != null) {
            throw new IllegalArgumentException("用户名已存在");
        }
        // 3、用户不存在，创建用户
        user = new User();
        user.setUsername(username);
        // 对明文密码加密，替换原明文赋值
        String encodedPassword = passwordEncoder.encode(password);
        user.setPassword(encodedPassword); // 存入加密后的密文
        user.setEmail(email);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        save(user);
        // 4、返回用户信息
        UserRegisterVO userRegisterVO = new UserRegisterVO();
        userRegisterVO.setId(user.getId());
        userRegisterVO.setUsername(user.getUsername());
        userRegisterVO.setEmail(user.getEmail());
        return userRegisterVO;
    }

    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {
        // 1、校验参数
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        // 2、根据用户名查询用户
        User user = lambdaQuery().eq(User::getUsername, username).one();
        if (user == null) {
            throw new BadRequestException("用户名不存在");
        }
        // 3、校验密码
        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new BadRequestException("密码错误");
        }
        // 4、生成token
        String token = jwtTool.createToken(user.getId(), jwtProperties.getTokenTTL());
        // 5、封装VO返回
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setToken(token);
        userLoginVO.setUserInfo(new UserInfo(user.getUsername(), user.getEmail()));
        return userLoginVO;
    }

}
